package com.auto.survey.activity.adjustment.adjustment_high

import android.content.Intent
import android.graphics.Color
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.auto.survey.R
import com.auto.survey.activity.adjustment.adjustment_high.dao.AdjustmentRepository
import com.auto.survey.activity.adjustment.adjustment_high.entity.Project
import com.auto.survey.base.BaseActivity
import com.auto.survey.util.Helper
import com.auto.survey.widget.CommonTitle.Companion.TEXT_POSITON_RIGHT
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * 高程网平差
 */
class HighAdjustMentActivity: BaseActivity() {

    private val repository = AdjustmentRepository()
    private val adapter = HighAdjustMentAdapter()
    override fun initView() {
        mTopBar?.visibility = View.VISIBLE
        mTopBar?.setBackgroundResource(R.color.blue)

        mComment_title?.visibility = View.VISIBLE
        mComment_title?.setBackgroundResource(R.color.blue)
        mComment_title?.setTitle("高程网平差工具")

        mComment_title?.setRightTitle("新建项目")
        mComment_title?.setTitleColor(Color.WHITE, TEXT_POSITON_RIGHT)
        mComment_title?.setOnRightTextClickListener(Helper.debounce{
            val intent = Intent()
            intent.setClass(this@HighAdjustMentActivity, HighAddActivity::class.java)
            startActivity(intent)
        })

        val recyclerView = findViewById<RecyclerView>(R.id.rv_adjustment_hight)
        recyclerView.layoutManager = LinearLayoutManager(this@HighAdjustMentActivity)

        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(object : ItemClickListener {
            override fun onClickItem(item: Project, position: Int) {
                val intent = Intent()
                intent.putExtra(HighDetailActivity.KEY_INTENT_PROJECT_ID, item.id)
                intent.putExtra(HighDetailActivity.KEY_INTENT_PROJECT_NAME, item.name)
                intent.setClass(this@HighAdjustMentActivity, HighDetailActivity::class.java)
                startActivity(intent)
            }
        })
    }

    override fun onResume() {
        super.onResume()

        CoroutineScope(Dispatchers.IO).launch {
            val datas = repository.queryAllProject()
            withContext(Dispatchers.Main) {
                adapter.setItems(datas.toMutableList())
            }
        }

    }

    override fun getLayoutId(): Int {
        return R.layout.activity_high_adjustment
    }
}